<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#666666">
  <tr bgcolor="#FDFDFD">
    <td width="12%" rowspan="2">Dia</td>
    <td colspan="2" bgcolor="#FDFDFD"><div align="center">Estatísticas</div></td>
    <td colspan="4" bgcolor="#FDFDFD"><div align="center">Registo</div></td>
    <td width="30%" rowspan="2" bgcolor="#FDFDFD"><div align="center"></div>
        <div align="center">Eventos</div></td>
  </tr>
  <tr bgcolor="#FDFDFD">
    <td width="10%" bgcolor="#FDFDFD">Tempo &Uacute;til</td>
    <td width="10%" bgcolor="#FDFDFD">DiferenÇa</td>
    <td width="10%" align="left" bgcolor="#FDFDFD">Entrada</td>
    <td width="10%" align="left" bgcolor="#FDFDFD">Saída</td>
    <td width="10%" align="left" bgcolor="#FDFDFD">AlmoÇo (Início)</td>
    <td width="10%" align="left" bgcolor="#FDFDFD">AlmoÇo (Regresso) </td>
  </tr>
  <?php
		  
		$CList = "SELECT 
						TIME(timeclock_ttables.entrytime) AS entrytime,
						TIME(timeclock_ttables.leavetime) AS leavetime,
						TIME(timeclock_ttables.lunchstart) AS lunchstart,
						TIME(timeclock_ttables.lunchend) AS lunchend,
						DATE(a.time_main) AS Time,
						TIME(a.InTime) AS InTime,
						TIME(a.OutTime) AS OutTi,
						TIME(a.LunchStart) AS LunchStart,
						TIME(a.LunchEnd) AS LunchEnd,
						timeclock_delays.ref AS Delay,
						e.name AS DelayType,
						timeclock_xtime.ref AS Comp
							FROM timeclock_operations AS a
							INNER JOIN timeclock_ttables ON timeclock_ttables.ref = a.RefTTable
							LEFT OUTER JOIN timeclock_xtime ON timeclock_xtime.refOperation = a.ref
							LEFT OUTER JOIN timeclock_delays ON timeclock_delays.refOperation = a.ref
							LEFT OUTER JOIN htck_types AS e ON timeclock_delays.RefType = e.StatRef AND e.Type = 2
							WHERE main_users_ref = {$_SESSION['UtID']} 
		  									AND YEAR(time_main) = {$ConsultTime[0]}
											AND MONTH(time_main) = {$ConsultTime[1]}
											GROUP BY a.ref
											ORDER BY time_main ASC";
		$List = $GLOBALS['CONN']->db_array($CList);
		$TAvTime = 0;
		$TToTime = 0;
		for ($m = 1; $m <= $List[0]; $m++) {
		if ($List[$m]['InTime'] == null) {
			$CVacs = "SELECT * FROM timeclock_vacs
						WHERE 
				'{$List[$m]['Time']}' BETWEEN DATE(TimeStart) AND DATE(TimeEnd) 
		AND timeclock_vacs.RefUser = {$_SESSION['UtID']}
		AND Status = 4";
		$Vacs = $GLOBALS['CONN']->db_array($CVacs);
		if (isset($Vacs[1])) {
			$Motv = 'De F�rias at� '.$Vacs[1]['TimeEnd'];
			} else {
			$Motv = 'NÃo Compareceu';
			}
		echo '<tr bgcolor="#FDFDFD"> 
            <td>'.$List[$m]['Time'].'</td> 
            <td bgcolor="#FDFDFD" colspan="8">'.$Motv.'</td>
			</tr>';
		} else {
		?>
  <tr bgcolor="#FDFDFD">
    <td><?php echo $List[$m]['Time'];?></td>
    <td bgcolor="#FDFDFD">
      <?php
	if ($List[$m]['OutTi'] == null) {
	$Eve = 112;
	$List[$m]['OutTi'] = $List[$m]['leavetime'];
	} if ($List[$m]['OutTi'] < $List[$m]['InTime']) {
	$AvTime = '0';
	} else {
	$AvTime = (strtotime($List[$m]['OutTi'])-strtotime($List[$m]['InTime']))-((strtotime($List[$m]['LunchEnd'])-strtotime($List[$m]['LunchStart'])));
	} echo date("H:i:s",$AvTime-3600);
	$TAvTime = $TAvTime+$AvTime;
	?></td>
    <td colspan="-1" bgcolor="#FDFDFD">
      <?php
	$ScTime = (strtotime($List[$m]['leavetime'])-strtotime($List[$m]['entrytime']))-((strtotime($List[$m]['lunchend'])-strtotime($List[$m]['lunchstart'])));
	$ToTime = $AvTime-$ScTime;
	$TToTime = $TToTime+$ToTime;
	if ($ToTime < 0) {
		echo '<font color="#FF0000">'.date("H:i:s",0-$ToTime-3600).'</font>';
	} else {
		echo date("H:i:s",$ToTime-3600);
	}
?></td>
    <td bgcolor="#FDFDFD"><?php echo $List[$m]['InTime'];?></td>
    <td bgcolor="#FDFDFD"><?php echo $List[$m]['OutTi'];			
	if (isset($Eve) && $Eve == 112) {
			echo '<img src="'.$GLOBALS['GFX'].'TimeClock/autoclose.png'.'" alt="Fecho Autom�tico">';
			};?></td>
    <td bgcolor="#FDFDFD"><?php echo $List[$m]['LunchStart'];?></td>
    <td bgcolor="#FDFDFD"><?php echo $List[$m]['LunchEnd'];?></td>
    <td bgcolor="#FDFDFD"><?php 
			if (isset($List[$m]['Delay']))  { 
			echo '<a href="?ACT=VCTS&Event=Delay&Ev='.$List[$m]['Delay'].'&Io='.$m.'">'.$List[$m]['DelayType'].'</a> ';}
			if (isset($List[$m]['Comp']))  {
			echo '<a href="?ACT=VCTS&Event=Comp&Ev='.$List[$m]['Comp'].'&Io='.$m.'">Compensação</a> ';}
			?></td>
  </tr>
  <?php 
  	if (isset($_GET['Ev']) && isset($_GET['Io']) && $m == $_GET['Io']) { 
	if (isset($_GET['Event']) && $_GET['Event'] == 'Comp') { 
		$CEvent = "SELECT
					a.Justification AS TypeName,
					DATE(a.EventTime) AS Date,
					Time(a.EventTime) AS Time,
					a.EventTime AS EvTime,
					d.name AS Status,
					n.name AS User,
					e.name AS Type
					
					 FROM timeclock_xtime AS a
				LEFT OUTER JOIN htck_types AS e ON e.StatRef = 4 AND e.Type = 2
				INNER JOIN timeclock_operations AS c ON c.ref = a.RefOperation
				INNER JOIN htck_status AS d ON d.StatRef = a.Status AND d.Type = 2
				INNER JOIN main_users AS n ON n.ref = c.main_users_ref
				WHERE a.ref = {$_GET['Ev']}";
	} elseif (isset($_GET['Event']) && $_GET['Event'] == 'Delay') {
		$CEvent = "SELECT b.ref AS TypeID,
					b.name AS TypeName,
					DATE(a.EventTime) AS Date,
					Time(a.EventTime) AS Time,
					a.delaytime AS EvTime,
					d.name AS Status,
					n.name AS User,
					n.Cod,
					n.Secc,
					n.CatProf,
					e.name AS Type,
					e.StatRef
					 FROM timeclock_delays AS a
				LEFT OUTER JOIN htck_delaytypes AS b ON b.ref = a.Justify
				LEFT OUTER JOIN htck_types AS e ON e.StatRef = a.RefType AND e.Type = 2
				INNER JOIN timeclock_operations AS c ON c.ref = a.RefOperation
				INNER JOIN htck_status AS d ON d.StatRef = a.Status AND d.Type = 2
				INNER JOIN main_users AS n ON n.ref = c.main_users_ref
				WHERE a.ref = {$_GET['Ev']}";
	}
	$Ev = $GLOBALS['CONN']->db_array($CEvent); 
	for ($r = 1; $r <= $Ev[0]; $r++) { ?>
  <tr bgcolor="#F7F7F7">
    <td colspan="8"><table width="100%"  border="0" cellspacing="1" cellpadding="3">
        <tr bgcolor="#FFFFFF">
          <td colspan="5"><strong>Tipo:
            <?php echo $Ev[$r]['Type'];?>
            <?php
			if (!isset($Ev[$r]['StatRef'])) {
			echo date("H:i:s",$Ev[$r]['EvTime']+3600);
			} else {
			echo date("H:i:s",$Ev[$r]['EvTime']-3600);
			}?>
</strong></td>
        </tr>
        <tr bgcolor="#FFFFFF">
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>Justificação:
          <?php echo $Ev[$r]['TypeName'];?></td>
          <td>&nbsp;</td>
          <td>Estado:
          <?php echo $Ev[$r]['Status'];?></td>
        </tr>
      </table></td>
  </tr>
 <?php }
 }
  $Eve = null;
  }
		  } ?>
  <tr bgcolor="#FDFDFD">
    <td>Média Mês</td>
    <td bgcolor="#FDFDFD"><?php echo date("H:i:s",$TAvTime/$List[0]);?></td>
    <td bgcolor="#FDFDFD">
      <?php $DifT = $TToTime/$List[0];
			if ($DifT < 0) {
				echo '<font color="#FF0000">'.date("H:i:s",0-$DifT-3600).'</font>';
			} else {
				echo date("H:i:s",$DifT-3600);
			} ?></td>
    <td colspan="5" bgcolor="#FDFDFD"></td>
  </tr>
  <tr bgcolor="#FDFDFD">
    <td colspan="2">Totais</td>
    <td bgcolor="#FDFDFD">
      <?php $DifT = $TToTime;
			if ($DifT < 0) {
				echo '<font color="#FF0000">'.date("H:i:s",0-$DifT-3600).'</font>';
			} else {
				echo date("H:i:s",$DifT-3600);
			} ?></td>
    <td colspan="5" bgcolor="#FDFDFD"></td>
  </tr>
</table>
